﻿using System;
using TMPro;
using UnityEngine;
using Yoozoo.Gameplay.RTS.Proto;
using Yoozoo.Mars.Got;
using Yoozoo.Framework.Manager.ResourceSystem.Runtime;
using Yoozoo.UI.YGUI;

namespace Yoozoo.Gameplay.RTS
{
    public class TsanTip : MonoBehaviour,IWebImageHandler
    {
        
        [SerializeField] public YImage PlayerFrame;
        [SerializeField] public YImage PlayerIcon;
        [SerializeField] private TMP_Text LangConfigText;
        
        private Animator annimator;
        private RectTransform m_rectTransform;
        private WebImageHandler _webImageHandler;
        public CanvasGroup CanvasGroup;
        
        private string m_spriteName;

        public RectTransform rectTransform
        {
            get
            {
                if (m_rectTransform == null)
                {
                    m_rectTransform = GetComponent<RectTransform>();
                }
                return m_rectTransform;
            }
        }

        public void OnDisable()
        {
            Dispose();
        }
        
        public void Dispose( )
        {
            WebImageManager.Instance.RemoveLoadWebImageHandler(this,m_spriteName,_webImageHandler);
            _webImageHandler = null;
        }
        
        public string spriteName
        {
            get { return m_spriteName; }
            set
            {
                if (m_spriteName == value)
                    return;
                if (WebImageManager.IsFromWeb(this.m_spriteName))
                {
                    WebImageManager.Instance.RemoveLoadWebImageHandler(this, this.m_spriteName, _webImageHandler);
                }
                    
                
                m_spriteName = value;
                
                if (WebImageManager.IsFromWeb(value))
                {
                    if (_webImageHandler == null) _webImageHandler = new WebImageHandler(this);
                    WebImageManager.Instance.AddLoadWebImageHandler(this, value, _webImageHandler);
                }
            }
        }
        
        public void LoadWebImageSuccess(Texture2D texture, string imageUrl)
        {
            if (this.spriteName == imageUrl && texture != null)
            {
                this.PlayerIcon.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
                    new Vector2(0.5f, 0.5f));
            }
        }
        
        public void LoadWebImageFail(string imageUrl)
        {
            Debug.LogError("TsanTip LoadWebImageFail  " + imageUrl);
        }

        public void SetTsanTip(bool isWebHead, string headPath, string framePath, string desc)
        {
            transform.GetComponent<Animation>().Play("ani_TsanTip_into");
            this.LangConfigText.text = desc;
            this.spriteName = "";
            if (isWebHead)
            {
                this.spriteName = headPath;
            }
            else
            {
                ArmyManager.Instance.ResourceManager.SetSpriteByPath(headPath, PlayerIcon);
            }
            ArmyManager.Instance.ResourceManager.SetSpriteByPath(framePath, PlayerFrame);
            
        }
        
    }
}
